﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using RuleCode.ObjectTree;

namespace SetupAction
{
    public partial class Form_SetupAction : Form
    {
        public Form_SetupAction()
        {
            InitializeComponent();
            TestBaseClass.AfterSetupHandler += new EventHandler(TestBaseClass_AfterSetupHandler);
        }

        void TestBaseClass_AfterSetupHandler(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            MessageBox.Show((sender as TestBaseClass).Name);
        }

        #region 按钮事件
        private void Button_OK_Click(object sender, EventArgs e)
        {
            string AddinFileName = "VSObjectTreeAddIn - For Testing.AddIn";
            string Vs2008DocumentPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Visual Studio 2008\\Addins";
            string FullFileName = Vs2008DocumentPath + "\\" + AddinFileName;
            string currentExcutiPath = Application.StartupPath;
            string sourceFullFileName = currentExcutiPath + "\\" + AddinFileName;
            string addInProjectName = "Project1.dll";
            string addInProjectFullName = currentExcutiPath + "\\" + addInProjectName;

            label1.Text = sourceFullFileName;
            label2.Text = FullFileName;
            File.Copy(sourceFullFileName, FullFileName, true);

            StreamReader sReader = new StreamReader(sourceFullFileName);
            string allXmlStr = sReader.ReadToEnd();
            sReader.Close();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(allXmlStr);
            XmlNodeList nodexs = xmlDocument.GetElementsByTagName ("Assembly");
            if (nodexs.Count >0)
                nodexs[0].InnerText = addInProjectFullName;
            xmlDocument.Save(FullFileName);
            this.Close();
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            //Form2 f2_0 = new Form2();
            //f2_0.Show();
            VaildatePer();
            
        }

        protected void VaildatePer()
        {
            string setupFilePath = "D:\\";
            //创建 info.ini 
            string infoFileName = Path.Combine(setupFilePath, "info.xml");
            if (!File.Exists(infoFileName))
                File.Create(infoFileName);
            //FileInfo fInfo = new FileInfo(infoFileName);
           // fInfo.Attributes = FileAttributes.Hidden | fInfo.Attributes;


            RuleCode.Common.Cryptography cry = new RuleCode.Common.Cryptography();
            XmlDocument infoXmlDoc = new XmlDocument();
            XmlNode infoNode = infoXmlDoc.CreateElement("info");
            XmlNode sDateNode = infoXmlDoc.CreateElement("sdate");
            string setupdateStr = cry.DesEncrypt(DateTime.Now.ToLongDateString(), Util.CryKey);
            sDateNode.InnerText = setupdateStr;
            infoNode.AppendChild(sDateNode);

            XmlNode lDateNode = infoXmlDoc.CreateElement("ldate");
            lDateNode.InnerText = setupdateStr;
            infoNode.AppendChild(lDateNode);

            XmlNode vDayNode = infoXmlDoc.CreateElement("vday");
            string validateDays = cry.DesEncrypt("30", Util.CryKey);
            vDayNode.InnerText = validateDays;
            infoNode.AppendChild(vDayNode);

            infoXmlDoc.AppendChild(infoNode);
            infoXmlDoc.Save(infoFileName);
        }

        private void Form_SetupAction_Load(object sender, EventArgs e)
        {
            TestBaseClass t1 = new TestBaseClass();
            t1.Name ="1";
            t1.InvokeAfterSetup();

            TestBaseClass t2 = new TestBaseClass();
            t2.Name = "22";
            t2.InvokeAfterSetup();
        }
    }


    public class TestBaseClass
    {
        public event EventHandler SetUpHandler;

        protected void OnSetUp()
        {
            if (SetUpHandler != null)
                SetUpHandler(this, EventArgs.Empty);
        }

        public static event EventHandler AfterSetupHandler;
        protected static void OnAfterSetup(object sender)
        {
            if (AfterSetupHandler != null)
                AfterSetupHandler(sender, EventArgs.Empty);
        }


        public string Name { get; set; }

        public void InvokeAfterSetup()
        {
            OnAfterSetup(this);
        }
    }
}
